<?php

	function nicknum(){
		$nick=md5(uniqid());
		$names=substr($nick,5,3);
		$name_bak=substr($nick,-3,-1);
		$loginn_names=$names.$name_bak;
		$rands=rand(1,4);
		if($rands==2){
			$loginn_names=strtoupper($loginn_names);
		}elseif($rands==3){
			$loginn_names=ucfirst($loginn_names);
		}else{
			$loginn_names=strtolower($loginn_names);
		}
		return $loginn_names;
	}

	function is_num($lei,$money){
    	$wei=bcmod($money*100,10);
    	if($lei==$wei){
    		return 1;
    	}else{
    		return 2;	
    	}
	}

   function hongbao($money, $number, $ratio = 0.5)
    {
        if ($money * 100 < $number) {
            //echo "max number:".$money*100;
            $map['state'] = 0;
            $map['msg'] = "max number:" . $money * 100;
            return $map;
            exit;
        }
        $tmep_money = $money;
        $res = array(); //结果数组
        $min = ($money / $number) * (1 - $ratio);   //最小值
        $max = ($money / $number) * (1 + $ratio);   //最大值
        /*--- 第一步：分配低保 ---*/
        for ($i = 0; $i < $number; $i++) {
            $res[$i] = $min;
        }
        $money = $money - $min * $number;
        /*--- 第二步：随机分配 ---*/
        $randRatio = 100;
        $randMax = ($max - $min) * $randRatio;
        for ($i = 0; $i < $number; $i++) {
            //随机分钱
            $randRes = mt_rand(0, $randMax);
            $randRes = $randRes / $randRatio;
            if ($money >= $randRes) { //余额充足
                $res[$i] += $randRes;
                $money -= $randRes;
            } elseif ($money > 0) {     //余额不足
                $res[$i] += $money;
                $money -= $money;
            } else {                   //没有余额
                break;
            }
        }
        /*--- 第三步：平均分配上一步剩余 ---*/
        if ($money > 0) {
            $avg = $money / $number;
            for ($i = 0; $i < $number; $i++) {
                $res[$i] += $avg;
            }
            $money = 0;
        }
        /*--- 第四步：打乱顺序 ---*/
        shuffle($res);
        /*--- 第五步：格式化金额(可选) ---*/
        foreach ($res as $k => $v) {
            //两位小数，不四舍五入
            preg_match('/^\d+(\.\d{1,2})?/', $v, $match);
            $match[0] = number_format($match[0], 2);
            $res[$k] = $match[0];
        }
        /*--- 第五步半：修复金额 ---*/
        $res = fixdata($tmep_money, $res);
        return $res;
	}
	
	
	
	function fixdata($tmep_money, $res){
      $sum=0;
        foreach ($res as $key => $vo) {
            if ($vo == 0) {
                $res[$key] = 0.01;
                $cha -= 0.01;
                $sum += 0.01;
            } else {
                $sum += $vo;
            }
        }
        $cha = round($tmep_money) - round($sum, 2);

        if ($cha > 0 && $cha < 0.01) {
            $cha = 0.01;
        }
        if ($sum != $tmep_money) {
            while ($cha != 0 && abs($cha) >= 0.01) {
                $n = mt_rand(0, count($res) - 1);
                if ($cha != 0 && abs($cha) >= 0) {
                    if ($cha > 0) {
                        $res[$n] += 0.01;
                        $cha -= 0.01;
                    } else {
                        if ($res[$n] > 0.01) {
                            $res[$n] -= 0.01;
                            $cha += 0.01;
                        }
                    }

                } else {
                    break;
                }
                $cha = round($cha, 2);
            }
        }
        return $res;
    }
	
  function pai_lei($money,$lei_wei){
	  do{
	    $red_list=hongbao($money,7);
	    $iflei=0;
	    foreach ($red_list as $key => $value) {
	     $wei=bcmod($value*100,10);
	      if ($wei==$lei_wei) {
	         $iflei=1;
	      }
	    }
	  }while($iflei==1);
	  return $red_list;
	}
	
	function two_leu($money,$lei_wei){
		do{
		    $red_list=hongbao($money,7);
		    $iflei=0;
		    foreach ($red_list as $key => $value) {
		     $wei=bcmod($value*100,10);
		      if ($wei==$lei_wei) {
		         $iflei+=1;
		      }
		    }
		 }while($iflei<=2);
		 return $red_list;
	}
	function dan_lei($money,$lei_wei){
		do{
		    $red_list=hongbao($money,7);
		    $iflei=0;
		    foreach ($red_list as $key => $value) {
		     $wei=bcmod($value*100,10);
		      if ($wei==$lei_wei) {
		         $iflei+=1;
		      }
		    }
		 }while($iflei<1);
		 return $red_list;
	}
	
	function sign1($data,$key){
	  $data = argSort1($data);
	  $data = createLinkstring1($data);
	  $sign = strtoupper(md5($data.'&key='.$key));
	  return $sign;
	}
	function argSort1($para) {

	  ksort($para);
	  reset($para);
	  return $para ;
	}
	function createLinkstring1($para) {

	  $arg  = "";

	  foreach ($para as $key=>$val) {
		$arg.=$key."=".$val."&";
	  }

	  $arg = substr($arg,0,strlen($arg)-1);

	  if( get_magic_quotes_gpc()){ $arg = stripslashes( $arg);}

	  return $arg;
	}
	function yunzhifu($value){
    $post_data = array (
        'appid'=>"560791e9-3b2d-4e48-8080-6af4dd94c7f5",
        'channel'=>"wx",
        'order_no'=>time()."_".rand(111,222),
        'amount'=>$value['jine'],
        'description'=>'红包到账啦!-' . $value['user'], 
        'recipient_openid'=>$value['txopenid']
    );
    
	$keys=sign1($post_data,"9fq0dw05looej3w64qrosnjbfbj8msg7");
    $post_data['sign'] = $keys;
	
    $url = 'http://api.0948.xyz/api/api/withdraw';

    $ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	// post数据
	curl_setopt($ch, CURLOPT_POST, 1);
	// post的变量
	curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
	
	$output = curl_exec($ch);
	curl_close($ch);
	
		//打印获得的数据
	    $output=json_decode($output,true);
	    //var_dump($output);die;
	    if($output['code']==40011){
	    	$info['code'] = '40011';
		    $info['data'] = $output;
		    $info['success'] = true;
		    $info['msg'] = $output['data']['return_code'];
		    return $info;
	    }else if($output['code']==40010){
	    	//$output['data']['return_code'] = 200;
	    	$info['code'] = '40010';
		    $info['data'] = $output;
		    $info['success'] = false;
		    $info['msg'] = $output['data'];
		    return $info;
	    }else{
	    	$info['code'] = '404';
	    	$info['success'] = false;
	        $info['msg'] = $output['errmsg'];
	        return $info;
	    }
	}
	
	